package hot100;


/*
 * Author：江松
 * Date：2023/4/8 14:02
 *
 合并二叉树:
以两棵树根为点向下构造新根，注意考虑为空的情况
 */

public class Main617 {
 public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }



    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1==null&&root2==null)return null;
        TreeNode root;
        if(root1==null&&root2!=null){
            root=new TreeNode(root2.val);
            root.left=mergeTrees(root1,root2.left);
            root.right=mergeTrees(root1,root2.right);
        }else if(root1!=null&&root2==null){
            root=new TreeNode(root1.val);
            root.left=mergeTrees(root1.left,root2);
            root.right=mergeTrees(root1.right,root2);
        }else{
            root=new TreeNode(root1.val+root2.val);
            root.left=mergeTrees(root1.left,root2.left);
            root.right=mergeTrees(root1.right,root2.right);
        }
        return root;
    }
}
